<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas绘画工具</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #f0f0f0;
            margin: 0;
            padding: 20px;
        }
        
        .toolbar {
            background-color: #fff;
            padding: 10px 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 10px;
            width: 800px;
            max-width: 100%;
        }
        
        .tool-group {
            display: flex;
            align-items: center;
            margin: 0 10px;
        }
        
        button {
            padding: 8px 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        button:hover {
            background-color: #45a049;
        }
        
        #clear {
            background-color: #f44336;
        }
        
        #clear:hover {
            background-color: #d32f2f;
        }
        
        label {
            margin-right: 8px;
            font-weight: bold;
        }
        
        input[type="range"] {
            width: 100px;
        }
        
        .color-picker {
            width: 30px;
            height: 30px;
            border: 2px solid #ddd;
            border-radius: 50%;
            cursor: pointer;
        }
        
        #canvas {
            background-color: white;
            border: 1px solid #ddd;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            cursor: crosshair;
        }
    </style>
</head>
<body>
    <h1>简易绘画工具</h1>
    
    <div class="toolbar">
        <div class="tool-group">
            <button id="clear">清空画布</button>
        </div>
        
        <div class="tool-group">
            <label for="brushSize">画笔大小:</label>
            <input type="range" id="brushSize" min="1" max="50" value="5">
            <span id="brushSizeValue">5</span>
        </div>
        
        <div class="tool-group">
            <label for="brushColor">颜色:</label>
            <input type="color" id="brushColor" value="#000000" class="color-picker">
        </div>
    </div>
    
    <canvas id="canvas" width="800" height="500"></canvas>
    
    <script>
        // 核心绘画功能实现
        class DrawingApp {
            constructor() {
                this.canvas = document.getElementById('canvas');
                this.ctx = this.canvas.getContext('2d');
                this.isDrawing = false;
                this.lastX = 0;
                this.lastY = 0;
                
                // 初始化工具
                this.initTools();
                // 初始化事件监听
                this.initEvents();
                // 初始化画布
                this.resetCanvas();
            }
            
            initTools() {
                // 工具元素
                this.brushSize = document.getElementById('brushSize');
                this.brushSizeValue = document.getElementById('brushSizeValue');
                this.brushColor = document.getElementById('brushColor');
                this.clearBtn = document.getElementById('clear');
                
                // 更新画笔大小显示
                this.brushSize.addEventListener('input', () => {
                    this.brushSizeValue.textContent = this.brushSize.value;
                });
                
                // 清空画布
                this.clearBtn.addEventListener('click', () => {
                    this.resetCanvas();
                });
            }
            
            initEvents() {
                // 鼠标事件
                this.canvas.addEventListener('mousedown', this.startDrawing.bind(this));
                this.canvas.addEventListener('mousemove', this.draw.bind(this));
                this.canvas.addEventListener('mouseup', this.stopDrawing.bind(this));
                this.canvas.addEventListener('mouseout', this.stopDrawing.bind(this));
                
                // 触摸事件
                this.canvas.addEventListener('touchstart', this.handleTouchStart.bind(this));
                this.canvas.addEventListener('touchmove', this.handleTouchMove.bind(this));
                this.canvas.addEventListener('touchend', this.stopDrawing.bind(this));
            }
            
            resetCanvas() {
                this.ctx.fillStyle = 'white';
                this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
                this.ctx.strokeStyle = this.brushColor.value;
                this.ctx.lineWidth = this.brushSize.value;
                this.ctx.lineCap = 'round';
                this.ctx.lineJoin = 'round';
            }
            
            startDrawing(e) {
                this.isDrawing = true;
                [this.lastX, this.lastY] = this.getPosition(e);
            }
            
            draw(e) {
                if (!this.isDrawing) return;
                
                const [x, y] = this.getPosition(e);
                
                this.ctx.strokeStyle = this.brushColor.value;
                this.ctx.lineWidth = this.brushSize.value;
                
                this.ctx.beginPath();
                this.ctx.moveTo(this.lastX, this.lastY);
                this.ctx.lineTo(x, y);
                this.ctx.stroke();
                
                [this.lastX, this.lastY] = [x, y];
            }
            
            stopDrawing() {
                this.isDrawing = false;
            }
            
            handleTouchStart(e) {
                e.preventDefault();
                const touch = e.touches[0];
                const mouseEvent = new MouseEvent('mousedown', {
                    clientX: touch.clientX,
                    clientY: touch.clientY
                });
                this.startDrawing(mouseEvent);
            }
            
            handleTouchMove(e) {
                e.preventDefault();
                const touch = e.touches[0];
                const mouseEvent = new MouseEvent('mousemove', {
                    clientX: touch.clientX,
                    clientY: touch.clientY
                });
                this.draw(mouseEvent);
            }
            
            getPosition(e) {
                const rect = this.canvas.getBoundingClientRect();
                return [
                    e.clientX - rect.left,
                    e.clientY - rect.top
                ];
            }
        }
        
        // 初始化应用
        document.addEventListener('DOMContentLoaded', () => {
            new DrawingApp();
        });
    </script>
</body>
</html>